
package com.probiz.estore.imports.handler.product;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductAttGroup;
import com.probiz.estore.common.model.product.ProductAttGroupItem;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.common.model.product.ProductTypeSkuOption;
import com.probiz.estore.common.model.product.SkuOptionValue;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProductManager;

public class ProductCodeHandler extends ColumnBasicHandler implements
		ColumnHandler {
	private Logger logger = Logger.getLogger(ProductCodeHandler.class);
	private ProductManager productManager;
	private ProdMainManager prodMainManager;
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}


	public void setProperty(ImportModel importModel,Column column) throws Exception {
		String value = column.getValue();
		if (StringUtils.isEmpty(value)) {
			return;
		}	
		Product product = productManager.getProductByProductCode(value);
		if (product != null) {
			if(product.getStatus().equals(Product.STATUS_LOGICAL_DELETED)){
				//设置结果
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "无法导入，找不到该商品编码指定的商品";
				logger.error(msg);
				importModel.setMsg(msg);
				return;
			}
			importModel.setUpdate(true);
			//start:将相关的延迟加载对象预先读取
			if(product.getProductDescription()!=null){
				product.getProductDescription().getFullDescription();
			}
			if(product.getProductStat()!=null){
				product.getProductStat().getOnShowReviewCount();
			}
			if(product.getProductSkus()!=null){
				product.getProductSkus().size();
				for(ProductSku sku:product.getProductSkus()){
					sku.getInventory().getQuantityOnHand();
					sku.getProductSkuOptionValues().size();
				}
			}
			if(product.getProductMedias()!=null){
				product.getProductMedias().size();
			}
			if(product.getProductType().getProductAttGroups()!=null){
				product.getProductType().getProductAttGroups().size();
				for(ProductAttGroup group : product.getProductType().getProductAttGroups()){
					group.getProductAttGroupItems().size();
					for(ProductAttGroupItem item: group.getProductAttGroupItems()){
						item.getAttribute().getAttributeName();
					}
				}
			}
			if(product.getProductType().getProductTypeBrands()!=null){
				product.getProductType().getProductTypeBrands().size();
			}
			if(product.getProductType().getProductTypeSkuOptions()!=null){
				product.getProductType().getProductTypeBrands().size();
				for(ProductTypeSkuOption productTypeSkuOption:product.getProductType().getProductTypeSkuOptions()){
					productTypeSkuOption.getSkuOption().getSkuOptionName();
					for(SkuOptionValue skuOptionValue:productTypeSkuOption.getSkuOption().getSkuOptionValues()){
						skuOptionValue.getSkuOptionValueName();
					}
				}
			}
			//end:将相关的延迟加载对象预先读取
		}else{
			importModel.setUpdate(false);
			product = new Product();
		}
		product.setProductCode(value);
		importModel.setTarget(product);
		importModel.setResult(ImportModel.RESULT_SUCCESS);
	}
}
